﻿/*
* 
* TEST = OK
*/
Library.UserInterface.Controls.DisablePaste = function() {
    return false;
};

/*
* 
* TEST = OK
*/
Library.UserInterface.Controls.Numeric = {
    KeyDown: function(e, elementId) {
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        var key = e.keyCode || e.which;
        var ret = false;
        var decimalLength = Library.UserInterface.GetAttribute(elementId, "DecimalLength");

        if (((key >= 35) && (key <= 40)) || ((key >= 48) && (key <= 57)) || ((key >= 96) && (key <= 105)) || key == 8 || key == 46 || key == 9 || key == 13 || (key == 188 && decimalLength != null && decimalLength > 0 && element.value.length > 0 && Library.Util.Count(element.value, ",") == 0))
            ret = true;

        return Library.Util.Return(e, ret);
    },

    Blur: function(e, elementId) {
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        var decimalLength = Library.UserInterface.GetAttribute(elementId, "DecimalLength");

        if (element.value.indexOf(",") > -1 && element.value.substr(element.value.indexOf(",") + 1).length > decimalLength) {
            element.value = element.value.substr(0, element.value.indexOf(",")) + "," + element.value.substr(element.value.indexOf(",") + 1, decimalLength);
        }

        var showDelimiters = Library.UserInterface.GetAttribute(elementId, "ShowDelimiters");
        if (showDelimiters == "True") {
            element.value = Library.Util.SayiFormatla(element.value);
        }
    },

    Focus: function(e, elementId) {
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        element.value = Library.Util.ReplaceAll(element.value, ".", "");
        Library.UserInterface.SetCursorPositionToEnd(elementId);
    }
};

/*
* 
* TEST = OK
*/
Library.UserInterface.Controls.IBAN = {
    KeyDown: function(e, elementId) {
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        var key = e.keyCode || e.which;
        var ret = false;
        var decimalLength = null;
        window.status = key;
        if (element.value.length > 2) {
            if (key == 32)
                ret = true;                
            else if (((key >= 35) && (key <= 40)) || ((key >= 48) && (key <= 57)) || ((key >= 96) && (key <= 105)) || key == 8 || key == 46 || key == 9 || key == 13 || (key == 188 && decimalLength != null && decimalLength > 0 && element.value.length > 0 && Library.Util.Count(element.value, ",") == 0))
                ret = true;
        }

        if (element.value.length < 2) {
            if (((key >= 35) && (key <= 40)) || ((key >= 48) && (key <= 57)) || ((key >= 96) && (key <= 105)) || key == 8 || key == 46 || key == 9 || key == 13 || (key == 188 && decimalLength != null && decimalLength > 0 && element.value.length > 0 && Library.Util.Count(element.value, ",") == 0))
                ret = false;
        }

        return Library.Util.Return(e, ret);
    },

    Blur: function(e, elementId) {
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        if (element.value.indexOf(" ") > -1)
            element.value = Library.Util.ReplaceAll(element.value, " ", "");

        element.value = element.value.substr(0, 4) + " " + element.value.substr(4, 4) + " " +
                        element.value.substr(8, 4) + " " + element.value.substr(12, 4) + " " +
                        element.value.substr(16, 4) + " " + element.value.substr(20, 4) + " " +
                        element.value.substr(24);
    },

    Focus: function(e, elementId) {
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        Library.UserInterface.SetCursorPositionToEnd(elementId);
    }
};

/*
* 
* TEST = OK
*/
Library.UserInterface.Controls.Combo = {
    Submit: function(e, pageName, islem) {
        Library.UserInterface.Submit(null, '', islem);
        document.forms[0].submit();
    }
};

/*
* 
* TEST = OK
*/
Library.UserInterface.Controls.Date = {
    KeyDown: function(e, elementId) {
        var key = e.keyCode || e.which;
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        var ret = false;
        
        if (((key >= 35) && (key <= 40)) || ((key >= 48) && (key <= 57)) || ((key >= 96) && (key <= 105)) || key == 8 || key == 46 || key == 9 || key == 13)
            ret = true;

        return Library.Util.Return(e, ret);
    },

    Blur: function(e, elementId) {
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        if (element.value.length == 8)
            element.value = element.value.substr(0, 2) + "." + element.value.substr(2, 2) + "." + element.value.substr(4);
    },

    Focus: function(e, elementId) {
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        if (element.value.length == 10)
            element.value = Library.Util.ReplaceAll(element.value, ".", "");
        Library.UserInterface.SetCursorPositionToEnd(elementId);
    }
};

/*
* 
* TEST = 
*/
Library.UserInterface.Controls.Time = {
    KeyDown: function(e, elementId) {
        var key = e.keyCode || e.which;
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        var ret = false;

        if (((key >= 35) && (key <= 40)) || ((key >= 48) && (key <= 57)) || ((key >= 96) && (key <= 105)) || key == 8 || key == 46 || key == 9 || key == 13)
            ret = true;

        return Library.Util.Return(e, ret);
    },

    Blur: function(e, elementId) {
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        if (element.value.length == 4)
            element.value = element.value.substr(0, 2) + ":" + element.value.substr(2, 2);
    },

    Focus: function(e, elementId) {
        var element = Library.UserInterface.GetDetailPageElement(elementId);
        element.value = Library.Util.ReplaceAll(element.value, ":", "");
        Library.UserInterface.SetCursorPositionToEnd(elementId);
    }
};

/*
* 
* TEST = 
*/
Library.UserInterface.Controls.GridView = {
    Check: function(elementName) {
        var checkAllControl = document.getElementById("checkAll" + elementName);
        var elements = document.getElementsByName("check_" + elementName);
        for (var i = 0; i < elements.length; i++) {
            elements[i].checked = checkAllControl.checked;
        }
    },

    PageSizeChanged: function(elementId) {
        var value = Library.UserInterface.GetDetailPageElement(elementId).value;
        __doPostBack(Library.Util.ReplaceAll(elementId, "PageSize", ""), "PageSizeChanged&PageSize=" + value);
    }
};

/*
* 
* TEST = 
*/
Library.UserInterface.Controls.TreeView = {
    Check: function (event) {
        var obj = Library.Util.SrcElement(event);
        var treeNodeFound = false;
        var checkedState;
        if ((obj.nodeName || obj.tagName) == "INPUT" && obj.type == "checkbox") {
            var treeNode = obj;
            checkedState = treeNode.checked;
            do {
                obj = obj.parentNode;
            } while (obj.tagName != "TABLE")
            var parentTreeLevel = obj.rows[0].cells.length;
            var parentTreeNode = obj.rows[0].cells[0];
            var tables = obj.parentNode.getElementsByTagName("TABLE");
            var numTables = tables.length
            if (numTables >= 1) {
                for (i = 0; i < numTables; i++) {
                    if (tables[i] == obj) {
                        treeNodeFound = true;
                        i++;

                        if (i == numTables)
                            i--;
                    }
                    if (treeNodeFound == true) {
                        var childTreeLevel = tables[i].rows[0].cells.length;
                        if (childTreeLevel > parentTreeLevel) {
                            var cell = tables[i].rows[0].cells[childTreeLevel - 1];
                            var inputs = cell.getElementsByTagName("INPUT");
                            inputs[0].checked = checkedState;
                            Library.UserInterface.Controls.TreeView.ParentCheck(treeNode, checkedState);
                        } else {
                            Library.UserInterface.Controls.TreeView.ParentCheck(treeNode, checkedState);
                            return;
                        }                        
                    }
                }
            }
        }
    },

    ParentCheck: function (treeNode, checkedState) {
        var obj = treeNode;
        do {
            obj = obj.parentNode;
        } while ((obj.nodeName || obj.tagName) != "DIV")

        if (!checkedState) {
            var sameLevelChecks = obj.getElementsByTagName("input");
            for (var i = 0; i < sameLevelChecks.length; i++) {
                if (sameLevelChecks[i].type == "checkbox" && sameLevelChecks[i].checked) {
                    checkedState = true;
                    break;
                }
            }
        }

        var ustCheckList = obj.parentNode.childNodes;
        var ustCheckListLenght = 999;
        var ustCheckTable = null;
        for (var j = 1; j < ustCheckListLenght; j++) {
            if (ustCheckList[j] == null || ustCheckList[j] == "undefined")
                break;
            if (ustCheckList[j] == obj) {
                ustCheckTable = ustCheckList[j - 1];
                break;
            }
        }

        var ustChecks = ustCheckTable.getElementsByTagName("input");
        for (var j = 0; j < ustChecks.length; j++) {
            if (ustChecks[j].type == "checkbox") {
                ustChecks[j].checked = checkedState;
                Library.UserInterface.Controls.TreeView.ParentCheck(ustChecks[j], checkedState);
            }
        }
    }
};